package com.qianli.user.application;

import com.fqgj.common.api.Response;
import com.google.common.collect.Lists;
import com.qianli.user.domain.enums.UserModelItemStatusEnum;
import com.qianli.user.domain.facatory.UserFrozenFactory;
import com.qianli.user.domain.model.UserFrozenModel;
import com.qianli.user.domain.model.frozen.UserFrozen;
import com.qianli.user.enums.FrozenCodeEnum;
import com.qianli.user.ro.UserStateCode;
import com.qianli.user.ro.frozen.UserFrozenRO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * name:用户冷冻
 * auth:ting.li@qianli-inc.com
 * date:2018/1/3 19:13
 * version: v1.0.0
 **/
@Service
public class UserFrozenApplication {

    @Autowired
    private UserFrozenFactory userFrozenFactory;
    /**
     * 冷冻用户
     * @param userFrozen
     * @return
     */
    public Response<Boolean> frozenUser(UserFrozenRO userFrozen) {
        //categoryCode校验，额度评估和借款都需要categoryCode
        if(null == userFrozen.getCategoryCode() &&
           (userFrozen.getCode().equals(FrozenCodeEnum.BORROW_REJECT.getCode())
           || userFrozen.getCode().equals(FrozenCodeEnum.EVALUATE_REJECT.getCode()))){
            return Response.error(UserStateCode.USER_FROZEN_NEED_APPOINT_CATEGORYCODE.getMsg());
        }
        //加载冷冻信息
        UserFrozenModel model = new UserFrozenModel();
        model.setUserCode(userFrozen.getUserCode());
        userFrozenFactory.load(model);
        //原有冷冻信息
        List<UserFrozen> frozens = model.getFrozens();
        //组装新冷冻信息
        UserFrozen newFrozen = packModel(userFrozen);
        UserFrozenModel newUm = new UserFrozenModel();
        if(CollectionUtils.isEmpty(frozens)){
            newFrozen.setItemStatus(UserModelItemStatusEnum.ADD.getType());
        }else{
            newFrozen.setItemStatus(UserModelItemStatusEnum.ADD.getType());
            for(UserFrozen f:model.getFrozens()){
                //额度评估和借款被拒冷冻校验产品类目，有则不操作
                if((f.getCode().equals(FrozenCodeEnum.EVALUATE_REJECT.getCode())
                   ||f.getCode().equals(FrozenCodeEnum.BORROW_REJECT.getCode()))
                        &&f.getCategoryCode().equals(newFrozen.getCategoryCode())){
                    newFrozen.setItemStatus(UserModelItemStatusEnum.DEFAULT.getType());
                }
                //账号冷冻和人脸识别冷冻有则不操作
                if((f.getCode().equals(FrozenCodeEnum.FROZEN.getCode())
                    ||f.getCode().equals(FrozenCodeEnum.FACE_RECONGNITION.getCode()))){
                    newFrozen.setItemStatus(UserModelItemStatusEnum.DEFAULT.getType());
                }
            }
        }
        newUm.setFrozens(Lists.newArrayList(newFrozen));
        //保存冷冻信息
        userFrozenFactory.store(newUm);
        return Response.ok(true);
    }

    /**
     * 组装模型
     * @param userFrozen
     * @return
     */
    private UserFrozen packModel(UserFrozenRO userFrozen) {
        UserFrozen frozen = new UserFrozen();
        frozen.setUserCode(userFrozen.getUserCode());
        frozen.setCategoryCode(userFrozen.getCategoryCode());
        frozen.setCode(userFrozen.getCode());
        frozen.setDesc(userFrozen.getDesc());
        //账号冷冻
        Integer frontDays = 0;
        if (userFrozen.getCode().equals(FrozenCodeEnum.BORROW_REJECT)) {//借款被拒冷冻
            frontDays = 14;
        } else if (userFrozen.getCode().equals(FrozenCodeEnum.FACE_RECONGNITION)) {//人脸识别冷冻
            frontDays = 7;
        } else if(userFrozen.getCode().equals(FrozenCodeEnum.EVALUATE_REJECT)){//额度评估冷冻
            frontDays = 30;
        }else if(userFrozen.getCode().equals(FrozenCodeEnum.FIRST_RISK_REJECT.getCode())){
            frontDays = 14;
        }else{
            frontDays = 365;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, frontDays);
        frozen.setFrozenEndDate(calendar.getTime());
        return frozen;
    }

    /**
     * 获取冷冻信息
     * @param userCode
     * @return
     */
    public Response<List<UserFrozenRO>> load(String userCode) {
        //加载冷冻信息
        UserFrozenModel userFrozenModel = new UserFrozenModel();
        userFrozenModel.setUserCode(userCode);
        userFrozenFactory.load(userFrozenModel);
        if(CollectionUtils.isEmpty(userFrozenModel.getFrozens())){
            return Response.ok(Lists.newArrayList());
        }

        List<UserFrozenRO> frozenROS = Lists.newArrayList();
        for(UserFrozen uf:userFrozenModel.getFrozens()){
            UserFrozenRO userFrozen = new UserFrozenRO();
            BeanUtils.copyProperties(uf,userFrozen);
            frozenROS.add(userFrozen);
        }
        return Response.ok(frozenROS);
    }

    /**
     * 查询指定冷冻信息
     * @param userCode
     * @param frozenCode
     * @return
     */
    public Response<Boolean> isFrozen(String userCode,FrozenCodeEnum frozenCode,String categoryCode){
        Response<List<UserFrozenRO>> frozenResponse = load(userCode);
        if(!frozenResponse.isSuccess()){
            return Response.ok(false);
        }
        List<UserFrozenRO> appointFrozens = Lists.newArrayList();
        List<UserFrozenRO> frozens = frozenResponse.getData();
        for(UserFrozenRO frozen:frozens){
            if(null == categoryCode &&
               frozenCode.getCode().equals(frozen.getCode())){
                appointFrozens.add(frozen);
            }
            if(null != categoryCode && categoryCode.equals(frozen.getCategoryCode())
               && frozenCode.getCode().equals(frozen.getCode())){
                appointFrozens.add(frozen);
            }
        }
        if(CollectionUtils.isNotEmpty(appointFrozens)){
            return Response.ok(true);
        }
        return Response.ok(false);
    }
}
